package com.xxxy.jsq.action.record;

import com.xxxy.jsq.dao.RecordDao;
import com.xxxy.jsq.model.Record;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/addRecordServlet")
public class addRecordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        Record model = new Record();
        RecordDao dao = new RecordDao();

        // 获取参数
        model.setRecordName(request.getParameter("recordName"));
        model.setRecordMemo(request.getParameter("recordMemo"));
        model.setRecordPeople("当前用户"); // 实际应从session获取
        model.setRecordPid(1L); // 实际应从session获取用户ID
        model.setRecordFlag("0"); // 默认启用

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            model.setRecordDate(sdf.parse(request.getParameter("recordDate")));
        } catch (ParseException e) {
            response.getWriter().print("日期格式错误");
            return;
        }

        model.setRecordCDate(new Date());

        String result = dao.add(model);
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close();
    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }
}